using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ipzip
{
    public partial class ZipProgressForm : Form
    {
        private Zipper zipper;

        public ZipProgressForm(Zipper zipper)
        {
            InitializeComponent();
            this.zipper = zipper;
            this.zipper.ProgressInfoCb = this.progressInfo;
        }

        private void zipBackgroundWorker_DoWork(object sender,DoWorkEventArgs e)
        {
            try
            {
                zipper.ScanAndZipFiles();
            }
            catch(Exception e0)
            {
                e.Result = e0;
            }
        }

        private void ZipProgressForm_Load(object sender,EventArgs e)
        {
            this.zipBackgroundWorker.RunWorkerAsync();
        }

        private void progressInfo(int percentage)
        {
            this.zipBackgroundWorker.ReportProgress(percentage);
        }

        private void zipBackgroundWorker_Completed(object sender,RunWorkerCompletedEventArgs e)
        {
            Exception e0;
            if((e0=e.Result as Exception)!=null)
            {

                Trace.TraceError("An error occured while creating the ZIP folder: " +
                    e0);
                MessageBox.Show(this,"An error occured while creating the ZIP folder:\n" +
                    e0.Message,Application.ProductName);

            }
            this.Dispose();
        }

        private void zipBackgroundWorker_ProgressChanged(object sender,ProgressChangedEventArgs e)
        {
            this.zipProgressBar.Value = e.ProgressPercentage;
        }
    }
}